package com.kjlink.internal.report4vue.report.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import com.kjlink.internal.report4vue.base.controller.BaseApiController;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.jira.model.Jira;
import com.kjlink.internal.workreport.jira.service.JiraService;
import com.kjlink.internal.workreport.project.service.ProjectService;
import com.kjlink.internal.workreport.report.lookup.WorkReportLookup;
import com.kjlink.internal.workreport.report.model.WorkReport;
import com.kjlink.internal.workreport.report.service.WorkReportService;
import com.kjlink.internal.workreport.report.vo.DayReportVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * 描述:
 * 工时填报
 *
 * @author Vic.xu
 * @see com.kjlink.internal.workreport.report.controller.WorkReportController
 * @since 2024-12-17 14:22
 */
@RestController
public class ApiReportController extends BaseApiController {

    @Resource
    private WorkReportService service;

    @Resource
    private ProjectService projectService;

    @Resource
    private JiraService jiraService;

    /**
     * 列表页
     */
    @RequestMapping(value = "/report/list", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<WorkReport>> list(@RequestBody WorkReportLookup lookup) {
        // 设置权限
        lookup.initAuthority();
        PageInfo<WorkReport> pager = service.page(lookup);
        return BaseResponse.success(pager);
    }

    /**
     * 研发经理可查看的日报列表：只能查看自己创建的项目相关的日报
     */
    @RequestMapping(value = "/report/devManagerList", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<WorkReport>> devManagerList(@RequestBody WorkReportLookup lookup) {
        // 设置权限
        lookup.markDevManager();
        PageInfo<WorkReport> pager = service.page(lookup);
        return BaseResponse.success(pager);
    }


    /**
     * 某日的详情
     *
     * @param id day对应的id
     */
    @GetMapping("/report/day/{id}")
    public BaseResponse<DayReportVo> dayDetail(@PathVariable Integer id) {
        DayReportVo dayReport = service.getDayReport(id);
        return BaseResponse.success(dayReport);
    }

    /**
     * 某日的详情
     *
     * @param date   日期
     * @param userId 用户id
     * @see #dayDetail(Integer)
     */
    @GetMapping("/report/day/detail")
    public BaseResponse<DayReportVo> dayDetail(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, Integer userId) {
        DayReportVo dayReport = service.getDayReportByDate(userId, date);
        return BaseResponse.success(dayReport);
    }


    /**
     * 删除
     */
    @PostMapping("/report/delete/{id}")
    public BaseResponse<Integer> delete(@PathVariable Integer id) {
        int num = service.delete(id);
        return BaseResponse.success(num);
    }

    /**
     * 批量异步json新增
     */
    @PostMapping("/report/batchInsert")
    public BaseResponse<?> batchInsert(@RequestBody List<WorkReport> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            BaseResponse<Boolean> checkJiraResult = jiraService.checkAndSaveJira(list);
            if (!checkJiraResult.isSuccess()) {
                return checkJiraResult;
            }
            Integer userId = SessionHelper.currentUserId();
            list.forEach(workReport -> {
                workReport.setUserId(userId);
                service.save(workReport);
                jiraService.summaryUsedTime(workReport.getJira());
            });
        }
        return BaseResponse.success();
    }

    /**
     * 更新
     */
    @PostMapping("/report/update")
    public BaseResponse<Integer> update(@RequestBody WorkReport workReport) {
        BaseResponse<Jira> checkCode = jiraService.createByCode(workReport.getJira(), workReport.getProjectId(), false);
        if (!checkCode.isSuccess()) {
            return BaseResponse.error(checkCode.getCode(), checkCode.getMsg());
        }
        int num = service.update(workReport);
        jiraService.summaryUsedTime(workReport.getJira());
        return BaseResponse.success(num);
    }


    /**
     * 导出
     */
    @RequestMapping(value = "/report/export")
    public void exportAll(WorkReportLookup lookup) throws IOException {
        lookup.initAuthority();
        service.export(response, lookup);
    }


}
